package com.example.myapplication.windowlive;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.example.myapplication.CommonUtils;
import com.example.myapplication.shortvideo.utils.DisplayUtils;

/**
 * @author: rongfeng
 * @date: 2021/1/11
 * @description
 */
public class TouchMoveView extends RelativeLayout implements View.OnTouchListener {

    /**
     * 记录X轴最后位置
     */
    private int lastX = 0;
    /**
     * 记录Y轴最后位置
     */
    private int lastY = 0;
    /**
     * 记录Action_Down - X轴位置
     */
    private int downX = 0;
    /**
     * 记录Action_Down - Y轴位置
     */
    private int downY = 0;
    /**
     * 记录Action_Down 时间
     */
    private long lastDownInMills;

    private int limitLeft;
    private int limitTop;
    private int limitRight;
    private int limitBottom;
    private int screenWidth;
    private int screenHeight;

    /**
     * 自适应边界
     */
    private boolean isfrist = true;

    int height;

    public TouchMoveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        screenWidth = metrics.widthPixels;
        screenHeight = metrics.heightPixels;

        height = DisplayUtils.dp2px(120);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        //获取SingleTouchView所在父布局的中心点
        ViewGroup mViewGroup = (ViewGroup) getParent().getParent();
        if (null != mViewGroup) {
//            mParentWidth = mViewGroup.getWidth();
//            mParentHeight = mViewGroup.getHeight();
            System.out.println(mViewGroup.getHeight());
        }

    }

    private void setLimitAuto() {
        if (isfrist) {
            this.limitLeft = DisplayUtils.dp2px(10);
            this.limitTop = DisplayUtils.dp2px(60);
            ViewGroup mViewGroup = (ViewGroup) getParent().getParent();
            if (null != mViewGroup) {
//            mParentWidth = mViewGroup.getWidth();
                screenHeight = mViewGroup.getHeight();
                System.out.println(mViewGroup.getHeight());
            }

            this.limitRight = screenWidth - DisplayUtils.dp2px(10);
            this.limitBottom = screenHeight - DisplayUtils.dp2px(60);
            isfrist = false;
            System.out.println("ssss screenHeight " + screenHeight);
            System.out.println("ssss StatusHeight " + DisplayUtils.getStatusHeight());


//
        }
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        setLimitAuto();
        RelativeLayout.LayoutParams lpFeedback;
        int marginRight, marginBottom;
        int dx, dy;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                downX = (int) event.getRawX();
                downY = (int) event.getRawY();
                lastDownInMills = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_MOVE:


//                System.out.println("ssss x " +  getLeft());
//                System.out.println("ssss y  " + getTop());
//
//                System.out.println("ssss b  " + getBottom());

                dx = (int) event.getRawX() - lastX;
                dy = (int) event.getRawY() - lastY;
                int left = getLeft() + dx;
                int top = getTop() + dy;
                int right = getRight() + dx;
                int bottom = getBottom() + dy;

                if (left < limitLeft) {
                    left = limitLeft;
                    right = left + getWidth();
                }
                if (top < limitTop) {
                    top = limitTop;
                    bottom = top + getHeight();
                }
                if (right > limitRight) {
                    right = limitRight;
                    left = limitRight - getWidth();
                }
                if (bottom > limitBottom) {
                    bottom = limitBottom;
                    top = limitBottom - getHeight();
                }


                viewLayout(left, top, right, bottom);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                lpFeedback = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

                marginRight = limitRight - left;
                marginBottom = limitBottom - top;

                lpFeedback.topMargin = getTop();
                lpFeedback.leftMargin = getLeft();
//                lpFeedback.setMargins(getLeft(), getTop(), -marginRight, -marginBottom);

                setLayoutParams(lpFeedback);

                return true;
            case MotionEvent.ACTION_UP:
                slideBorderAnim();
//                if (getLeft() + getWidth() / 2 > screenWidth / 2) {
//                    viewLayout(limitRight - getWidth(), getTop(), limitRight, getBottom());
//                    marginRight = limitRight - getLeft();
//                } else {
//                    viewLayout(limitLeft, getTop(), limitLeft + getWidth(), getBottom());
//                    marginRight = limitRight - limitLeft;
//                }
//                lpFeedback = new LayoutParams(
//                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//
//                marginBottom = limitBottom - getTop();
//                lpFeedback.setMargins(getLeft(), getTop(), -marginRight, -marginBottom);
//                setLayoutParams(lpFeedback);


                dx = (int) event.getRawX() - downX;
                dy = (int) event.getRawY() - downY;


                if (Math.abs(dx) < 10 && Math.abs(dy) < 10 && System.currentTimeMillis() - lastDownInMills < 500 && !CommonUtils.isFastDoubleClick()) {
                    System.out.println("ssss 点击");
                }


                break;
            default:
                break;
        }
        return true;
    }

    int lastTop;

    private void slideBorderAnim() {
        int startLeft, endLeft;
        startLeft = getLeft();
        if (startLeft + getWidth() / 2 > screenWidth / 2) {
            endLeft = limitRight - getWidth();
        } else {
            endLeft = limitLeft;
        }
        RelativeLayout.LayoutParams layoutParams = (LayoutParams) getLayoutParams();
        ValueAnimator anim = ValueAnimator.ofInt(startLeft, endLeft);
        anim.setDuration(150);
        anim.setRepeatCount(0);
        anim.addUpdateListener(animation -> {
            layoutParams.leftMargin = (Integer) animation.getAnimatedValue();
            setLayoutParams(layoutParams);
        });
        anim.start();
        lastTop = getTop();
    }


    private void viewLayout(int left, int top, int right, int bottom) {
        layout(left, top, right, bottom);
    }

    public void setInputViewLayoutListener(View decorView) {
        decorView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (top != oldTop) {
                    RelativeLayout.LayoutParams layoutParams = (LayoutParams) getLayoutParams();
                    if (getTop() + height > top) {
                        layoutParams.topMargin = top - height - DisplayUtils.dp2px(10);
                    } else {
                        layoutParams.topMargin = lastTop;
                    }
                    setLayoutParams(layoutParams);
                    if (oldTop > top) {
                        limitBottom = bottom - DisplayUtils.dp2px(60);
                    } else {
                        limitBottom = screenHeight - DisplayUtils.dp2px(60);
                    }
                }
            }
        });
    }


}
